package com.hxx.widget.richtext.views

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.text.Layout
import android.text.Spanned
import android.text.style.BulletSpan

class NumberBulletSpan(private val number: Int) : BulletSpan() {
    private val mGapWidth = 40
    private val mBulletRadius = 20

    override fun getLeadingMargin(first: Boolean): Int {
        return 2 * mBulletRadius + mGapWidth;
    }

    override fun drawLeadingMargin(
        canvas: Canvas,
        paint: Paint,
        x: Int,
        dir: Int,
        top: Int,
        baseline: Int,
        bottom: Int,
        text: CharSequence,
        start: Int,
        end: Int,
        first: Boolean,
        layout: Layout?
    ) {
        if ((text as Spanned).getSpanStart(this) == start) {
            val style = paint.style
            val oldcolor = paint.color
            paint.color = Color.BLACK
            paint.style = Paint.Style.FILL
            val fontMetrics = paint.fontMetrics
            val yPosition =
                (top + bottom) / 2f + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
            val textWidth = paint.measureText("-")
            val xPosition: Float = (x + mBulletRadius + mBulletRadius).toFloat() - textWidth / 2
            canvas.drawText("-", xPosition, yPosition, paint)
            paint.color = oldcolor
            paint.style = style
        }
    }
}